﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using System.Xml.Linq;
using Gleed2D.InGame;
using Microsoft.Xna.Framework.Graphics;

namespace Somnus.Utils
{
    class GleedToArray
    {

        public static List<Dictionary<String, Object>> getLevelList(String LevelName) 
        {
            Stream stream = TitleContainer.OpenStream("Content/Levels/" + LevelName + ".gleed");
            XElement xml = XElement.Load(stream);
            Level level = LevelLoader.Load(xml);

            List<Dictionary<String, Object>> levelList = new List<Dictionary<String, Object>>();


            foreach (Layer layer in level.Layers)
            {
                foreach (LayerItem item in layer.Items)
                {
                    Dictionary<String, Object> objeto = new Dictionary<String, Object>();

                    objeto["name"] = item.Properties.Name;
                    objeto["x"] = item.Properties.Position.X;
                    objeto["y"] = item.Properties.Position.Y;
                    

                    foreach (KeyValuePair<String, CustomProperty> customProperty in item.Properties.CustomProperties)
                    {
                        objeto[customProperty.Key] = customProperty.Value.Value;
                    }

                    if (item.ItemType == typeof(RectangleItemProperties))
                    {
                        objeto["rotation"] = item.RectangleItemProperies.Rotation;
                        objeto["width"] = item.RectangleItemProperies.Width;
                        objeto["height"] = item.RectangleItemProperies.Height;
                    }
                    else if (item.ItemType == typeof(TextureItemProperties))
                    {
                        objeto["rotation"] =  MathHelper.ToDegrees(item.TextureItemProperties.Rotation);
                        objeto["width"] = item.TextureItemProperties.Scale.X * 100;
                        objeto["height"] = item.TextureItemProperties.Scale.Y * 100;
                    }

                    objeto["texture"] = Game1.getInstance().Content.Load<Texture2D>("bringel");

                    levelList.Add(objeto);
                }
            }

            




            return levelList;
        }
    }
}
